// pages/notice/projectTeam/projectTeamMember/projectTeamMember.js

import { subscribe } from '../../../../utils/tools';
import throttle from '../../../../utils/throttle.js';
import Http from '../../../../utils/http.js';
const http = new Http();
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        leader: [],
        member: [],
        projectTeam: {},
        permissionList: ['00', '01'],
        hasSpecialIdentity: false,
        // 项目组权限
        projectauthority: '',
        //项目组是否订阅
        subscribeflg: '00',
    },

    changeNav: throttle(
        (e) => {
            let nav = e.currentTarget.dataset.nav;
            console.log(nav);
            wx.navigateTo({
                url: nav,
            });
        },
        1000,
        {
            trailing: false,
        }
    ),
    // 查看他人主页
    jumpOthersPage(e) {
        wx.navigateTo({
            url: `../../../../functionPage/pages/othersHomePage/othersHomePage?uid=${e.currentTarget.dataset.uid}`,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            id: options.id,
        });
        this.getProjectTeamDetail();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    refresh() {
        this.setData({
            leader: [],
            member: [],
            projectTeam: {},
        });
        this.getProjectTeamDetail();
    },
    getProjectTeamDetail() {
        let _that = this;
        let data = {
            authId: app.globalData.authId,
            id: _that.data.id,
        };
        http.getRequest(`cjMap/getProjectTeamDetail`, data).then((res) => {
            if (res.status == 0) {
                _that.setData({
                    leader: res.leader,
                    member: res.member || [],
                    projectTeam: res.projectTeam,
                    hasSpecialIdentity: res.projectauthority === '02',
                    subscribeflg: res.projectTeam.subscribeflg,
                });
            }
        });
    },
    //关注
    subscribeBtn() {
        let _that = this;
        let flg = '00';
        if (_that.data.subscribeflg == '00') {
            flg = '01';
        } else {
            if (
                _that.data.projectauthority == '02' ||
                _that.data.projectauthority == '03'
            ) {
                wx.showToast({
                    title: '项目组成员不可以取消关注哦！',
                    icon: 'none',
                    duration: 2000,
                });
                return;
            }
        }
        subscribe(_that.data.id, flg, function () {
            wx.showToast({
                title: flg == '00' ? '已取消关注' : '关注成功',
                icon: 'none',
                duration: 2000,
            });
            _that.setData({
                subscribeflg: flg,
            });
            let pages = getCurrentPages(); //当前页面栈
            let prevPage = pages[pages.length - 2]; //上一页面

            prevPage.setData({
                subscribeflg: flg,
            });
        });
    },
});
